// editor/command/workspaceCommand/LogShowCommand.java
package org.example.com.editor.command.logCommand;

import org.example.com.editor.command.Command;
import org.example.com.editor.logger.LogManager;
import org.example.com.editor.ui.ConsoleUI;
import org.example.com.editor.workspace.Workspace;

import java.io.File;
import java.io.IOException;

public class LogShowCommand implements Command {
    private final LogManager logManager;

    public LogShowCommand(LogManager logManager) {
        this.logManager = logManager;
    }

    @Override
    public void execute(Workspace workspace, ConsoleUI ui) {
        String[] args = ui.getCommandArgs();
        String targetFile;

        if (args.length == 1) {
            targetFile = workspace.getActiveFile();
            if (targetFile == null) {
                ui.printError("没有活动文件，请指定文件名或打开一个文件");
                return;
            }
        } else if (args.length == 2) {
            targetFile = args[1];
            try {
                targetFile = new File(targetFile).getCanonicalPath();
            } catch (IOException e) {
                ui.printError("不存在该路径文件");
            }
            if (!workspace.getOpenedFiles().contains(targetFile)) {
                ui.printError("文件未打开: " + targetFile);
                return;
            }
        } else {
            ui.printError("用法: log-show [file]");
            return;
        }

        logManager.showLog(targetFile, ui);
    }

    @Override
    public String getName() {
        return "log-show";
    }
}